-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix parallel tool call limit enforcement #2978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
c0165b0
to
5280163
Compare
@tradeqvest Since we'll raise an error regardless, is it worth executing tool calls up to the limit, instead of just raising immediately? I was thinking we could do something like this: pydantic-ai/pydantic_ai_slim/pydantic_ai/_agent_graph.py Lines 476 to 484 in bfcccba
Where we optimistically increment a copied version of the usage, and check the usage limit against that. |
@DouweM It would definitely be simpler, yet I was thinking that the tool output up until the UsageLimit violation could still be of value, captured and further processed. Let me know what you think. |
@tradeqvest I think it'd be misleading if those results never get sent back to the model to use, and the user will think their action failed even though some tools (with side effects) may have in fact been executed. If we had a way to, instead of failing hard, tell the model "this call was not executed because you hit the limit" for the calls over the limit, executing the earlier ones makes sense, but until we have such a mode I'd rather not run the tools at all. |
9a459ab
to
f053b90
Compare
@DouweM Good point! I've adapted the implementation to use the fail-fast approach you suggested. |
- Removed the unused `parts` variable in `UserPromptNode`.
- Inline limit check in _call_tools instead of separate function - Pass usage directly as parameter rather than extracting from tool_manager - Remove redundant per-tool check in ToolManager - Align error message format with other usage limit errors
- Simplified the tool call logic by removing the unused usage_limits parameter from the _call_tool method in ToolManager.
127eee7
to
6018ae1
Compare
@tradeqvest Thanks Niko! |
Fix parallel tool call limit enforcement
Problem
The
tool_calls_limit
inUsageLimits
was not properly enforced for parallel tool execution. When multiple tools were called in parallel, all tools would start executing before the limit was checked, allowing the limit to be exceeded before raisingUsageLimitExceeded
.For example, if
tool_calls_limit=6
and the model returned 8 parallel tool calls, all 8 tools would start executing before the error was raised.Solution
This PR modifies the parallel tool execution logic in
_agent_graph.py
to enforce the limit before starting tool tasks:UsageLimitExceeded
if the requested tool amount would violate the usage limit